package cn.lsj.lock.client;


import cn.lsj.lock.config.DataSource;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class ZKClientFactory {

    private static final ExponentialBackoffRetry DEFAULT_RETRY_POLICY = new ExponentialBackoffRetry(1000, 3);

    private ZKClientFactory() {
        throw new IllegalStateException();
    }


    public static ZKClient getInstance(DataSource dataSource) {
        return getInstance(dataSource, DEFAULT_RETRY_POLICY);
    }

    public static ZKClient getInstance(DataSource dataSource, RetryPolicy retryPolicy) {
        CuratorFramework client = CuratorFrameworkFactory.newClient(dataSource.getUrl() + ":" + dataSource.getPort(), retryPolicy);
        return new DefaultZKClient(client);
    }


}
